#include "AbstractLock.h"
#include <QStringList>

const char *RoomNumber = "RoomNumber";
const char *CardNumber = "CardNumber"; 
const char *User = "User";
const char *Operator = "Operator"; 
const char *CheckInDate = "CheckInDate";  
const char *CheckOutDate = "CheckOutDate";
const char *Loss = "Loss";
const char *ULock = "ULock";

AbstractLock::AbstractLock()
{
}
	
AbstractLock::~AbstractLock()
{
}

std::string AbstractLock::gbkToUtf8(std::string str)
{
	QString tmp = QString::fromLocal8Bit(str.c_str());
	return tmp.toUtf8().data();
}

std::string AbstractLock::utf8ToGbk(std::string str)
{
	QString tmp = QString::fromUtf8(str.c_str());
	return tmp.toLocal8Bit().data();
}

bool AbstractLock::CheckJsonParam(Json::Value value,QStringList paramList,QVector<Json::ValueType> valueList,QString &error)
{
	if(!value.isObject())
		return false; 

	Json::Value::Members membs = value.getMemberNames();

	Json::Value empty;
	for(int i = 0; i < paramList.size(); i++)
	{
		QString paramName = paramList[i];
		Json::Value tmp = value.get(qPrintable(paramName),empty);
		if(tmp.isNull())
		{
			error = QString("loss param:" + paramName);
			return false;
		}
		if(!tmp.isConvertibleTo(valueList[i]))
		{
			error = "param " + paramName + QString(" type error");
			return false;
		}
	}

	return true;
}

QDateTime AbstractLock::stampToDateTime(int timestamp)
{
	QDateTime time = QDateTime::fromMSecsSinceEpoch((qint64)timestamp * 1000);
	return time;
}

int AbstractLock::dateTimeToStamp(QDateTime time)
{
	if(time.isValid())
	{
		qint64 timestamp = time.toMSecsSinceEpoch();
		return int(timestamp / 1000);
	}
	else
		return 0;
}